
package shopping

import (
    "errors"
    "strings"
	"dhfshop/models"
	"dhfshop/models/sale"
	"github.com/astaxie/beego/orm"
)

type Favorites struct {
	FavId     int64     `orm:"column(fav_id);pk;auto" json:"fav_id" form:"fav_id"`  // 
	BuyerId   int64     `orm:"column(buyer_id);index" json:"buyer_id" form:"buyer_id"`     //买家member_id
	ItemId    int64     `orm:"column(item_id)" json:"item_id,string" form:"-"`     //
	ItemIdStr string    `orm:"-" json:"-" form:"item_id"`     //
	Title     string    `orm:"-" json:"title" form:"-"`          //不在此数据表中存储,联表查询
	Price     float32   `orm:"-" json:"price" form:"-"`          //不在此数据表中存储,联表查询
	Image     string    `orm:"-" json:"image" form:"-"`          //不在此数据表中存储,联表查询
}

func (u *Favorites) TableUnique() [][]string {
	return [][]string{
		[]string{"BuyerId", "ItemId"},
	}
}

func init() {
    orm.RegisterModel(new(Favorites))   
}

func GetFavorites(buyerId int) (lists []*Favorites, err error) {
	o := models.GetDefaultDB()
	_, err = o.QueryTable(new(Favorites)).Filter("buyer_id", buyerId).OrderBy("-fav_id").All(&lists)
	for _, favorite := range lists {
	    if item, err := sale.GetItemByItemId(favorite.ItemId); err == nil {
		    favorite.Title = item.Title
		    favorite.Price = item.Price
		    favorite.Image = item.Image
		}
	}
	return
}

func GetAllFavorites(query map[string]string,fields []string,order_by map[string]string,offset int,limit int)([]*Favorites, int64,error) {
	o := models.GetDefaultDB()
	qs := o.QueryTable(new(Favorites))
	for k, v := range query {
		k = strings.Replace(k, ".", "__", -1)
		if ok := strings.Contains(v, "|"); ok {
		    valueArr := strings.Split(v, "|")
			qs = qs.Filter(k, valueArr)
		}else{
		    qs = qs.Filter(k, v)
		}	
	}
	cnt, err := qs.Count()
	var sortFields []string
	for k, v := range order_by {
		orderby := ""
		if v == "desc" {
			orderby = "-" + k
		} else if v == "asc" {
			orderby = k
		} else {
			return nil, 0, errors.New("Error: Invalid order. Must be either [asc|desc]")
		}
		sortFields = append(sortFields, orderby)
	}
	qs = qs.OrderBy(sortFields...)
	var lists []*Favorites
	if _, err = qs.Limit(limit, offset).All(&lists, fields...); err == nil {
	    for _, favorite := range lists {
			if item, err := sale.GetItemByItemId(favorite.ItemId); err == nil {
				favorite.Title = item.Title
				favorite.Price = item.Price
				favorite.Image = item.Image
			}
	    }
		return lists, cnt, nil
	}
	return nil, 0, err
}

// 添加收藏夹项
func AddFavorites(v *Favorites) (id int64, err error) {
	o := models.GetDefaultDB()
    id, err = o.Insert(v)
	return
}

// 更新收藏夹项
func UpdateFavorites(v *Favorites) (id int64, err error) {
	o := models.GetDefaultDB()
	m := Favorites{FavId: v.FavId}
	if err = o.Read(&m); err == nil {
		if id, err = o.Update(v); err == nil {
		   return
		}
	}
	return
}

// 移出收藏夹项
func RemoveFavoritesItem(id int) (err error) {
    o := models.GetDefaultDB()
	_, err = o.QueryTable(new(Favorites)).Filter("fav_id", id).Delete()
	return
}

// 清空收藏夹
func DeleteFavorites(buyer_id int) (err error) {
    o := models.GetDefaultDB()
	_, err = o.QueryTable(new(Favorites)).Filter("buyer_id", buyer_id).Delete()
	return
}

